package reittiopas;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class ReittiopasMIDlet extends MIDlet implements CommandListener {

    private Command exitCommand;
    private Command getRouteCommand;
    private Display display;
    private Form mainForm;
    private Form resultForm;
    private TextField startAddressField;
    private TextField destAddressField;

    public ReittiopasMIDlet() {
        exitCommand = new Command("Exit", Command.EXIT, 0);
        getRouteCommand = new Command("Get Route", Command.OK, 0);
        display = Display.getDisplay(this);
        mainForm = new Form ("Get route");
        startAddressField = new TextField("Start address", "Kumpula", 30, TextField.ANY);
        destAddressField = new TextField("Destination address", "Keskusta", 30, TextField.ANY);

        mainForm.append(startAddressField);
        mainForm.append(destAddressField);
        mainForm.addCommand(exitCommand);
        mainForm.addCommand(getRouteCommand);
        mainForm.setCommandListener(this);

        resultForm = new Form ("Route");
        resultForm.addCommand(exitCommand);
        resultForm.setCommandListener(this);
    }

    public void startApp() {
        display.setCurrent(mainForm);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void commandAction(Command c, Displayable s) {
        if (c == exitCommand) {
            destroyApp(false);
            notifyDestroyed();
        }
        else if (c == getRouteCommand) {
            routeScreen();
        }
    }

    private void routeScreen() {
        RouteService rs = new RouteService();
        String res = rs.getViaHttpConnection("http://api.reittiopas.fi/public-ytv/fi/api/?a=2553648,6677498&b=2549445,6675513&user=jazuju&pass=s2fxru4w"); //http://cs.helsinki.fi/u/jkjuvone/
        //System.out.println(res);
        resultForm.append(res.substring(0, 1000));
        display.setCurrent(resultForm);
    }

}